package com.apollographql.apollo.internal.fetcher;

import com.apollographql.apollo.api.internal.ApolloLogger;
import com.apollographql.apollo.api.internal.Optional;
import com.apollographql.apollo.exception.ApolloException;
import com.apollographql.apollo.fetcher.ResponseFetcher;
import com.apollographql.apollo.interceptor.ApolloInterceptor;
import com.apollographql.apollo.interceptor.ApolloInterceptorChain;
import java.util.concurrent.Executor;

public final class CacheAndNetworkFetcher implements ResponseFetcher {
    @Override // com.apollographql.apollo.fetcher.ResponseFetcher
    public ApolloInterceptor provideInterceptor(ApolloLogger apolloLogger) {
        return new CacheAndNetworkInterceptor();
    }

    private static final class CacheAndNetworkInterceptor implements ApolloInterceptor {
        private Optional<ApolloException> cacheException;
        private Optional<InterceptorResponse> cacheResponse;
        private boolean dispatchedCacheResult;
        private volatile boolean disposed;
        private Optional<ApolloException> networkException;
        private Optional<InterceptorResponse> networkResponse;
        private CallBack originalCallback;

        private CacheAndNetworkInterceptor() {
            this.cacheResponse = Optional.absent();
            this.networkResponse = Optional.absent();
            this.cacheException = Optional.absent();
            this.networkException = Optional.absent();
        }

        @Override // com.apollographql.apollo.interceptor.ApolloInterceptor
        public void interceptAsync(InterceptorRequest request, ApolloInterceptorChain chain, Executor dispatcher, final CallBack callBack) {
            if (!this.disposed) {
                this.originalCallback = callBack;
                chain.proceedAsync(request.toBuilder().fetchFromCache(true).build(), dispatcher, new CallBack() {
                    /* class com.apollographql.apollo.internal.fetcher.CacheAndNetworkFetcher.CacheAndNetworkInterceptor.AnonymousClass1 */

                    @Override // com.apollographql.apollo.interceptor.ApolloInterceptor.CallBack
                    public void onResponse(InterceptorResponse response) {
                        CacheAndNetworkInterceptor.this.handleCacheResponse(response);
                    }

                    @Override // com.apollographql.apollo.interceptor.ApolloInterceptor.CallBack
                    public void onFailure(ApolloException e) {
                        CacheAndNetworkInterceptor.this.handleCacheError(e);
                    }

                    @Override // com.apollographql.apollo.interceptor.ApolloInterceptor.CallBack
                    public void onCompleted() {
                    }

                    @Override // com.apollographql.apollo.interceptor.ApolloInterceptor.CallBack
                    public void onFetch(FetchSourceType sourceType) {
                        callBack.onFetch(sourceType);
                    }
                });
                chain.proceedAsync(request.toBuilder().fetchFromCache(false).build(), dispatcher, new CallBack() {
                    /* class com.apollographql.apollo.internal.fetcher.CacheAndNetworkFetcher.CacheAndNetworkInterceptor.AnonymousClass2 */

                    @Override // com.apollographql.apollo.interceptor.ApolloInterceptor.CallBack
                    public void onResponse(InterceptorResponse response) {
                        CacheAndNetworkInterceptor.this.handleNetworkResponse(response);
                    }

                    @Override // com.apollographql.apollo.interceptor.ApolloInterceptor.CallBack
                    public void onFailure(ApolloException e) {
                        CacheAndNetworkInterceptor.this.handleNetworkError(e);
                    }

                    @Override // com.apollographql.apollo.interceptor.ApolloInterceptor.CallBack
                    public void onCompleted() {
                    }

                    @Override // com.apollographql.apollo.interceptor.ApolloInterceptor.CallBack
                    public void onFetch(FetchSourceType sourceType) {
                        callBack.onFetch(sourceType);
                    }
                });
            }
        }

        @Override // com.apollographql.apollo.interceptor.ApolloInterceptor
        public void dispose() {
            this.disposed = true;
        }

        /* access modifiers changed from: package-private */
        public synchronized void handleNetworkResponse(InterceptorResponse response) {
            this.networkResponse = Optional.of(response);
            dispatch();
        }

        /* access modifiers changed from: package-private */
        public synchronized void handleNetworkError(ApolloException exception) {
            this.networkException = Optional.of(exception);
            dispatch();
        }

        /* access modifiers changed from: package-private */
        public synchronized void handleCacheResponse(InterceptorResponse response) {
            this.cacheResponse = Optional.of(response);
            dispatch();
        }

        /* access modifiers changed from: package-private */
        public synchronized void handleCacheError(ApolloException exception) {
            this.cacheException = Optional.of(exception);
            dispatch();
        }

        private synchronized void dispatch() {
            if (!this.disposed) {
                if (!this.dispatchedCacheResult) {
                    if (this.cacheResponse.isPresent()) {
                        this.originalCallback.onResponse(this.cacheResponse.get());
                        this.dispatchedCacheResult = true;
                    } else if (this.cacheException.isPresent()) {
                        this.dispatchedCacheResult = true;
                    }
                }
                if (this.dispatchedCacheResult) {
                    if (this.networkResponse.isPresent()) {
                        this.originalCallback.onResponse(this.networkResponse.get());
                        this.originalCallback.onCompleted();
                    } else if (this.networkException.isPresent()) {
                        this.originalCallback.onFailure(this.networkException.get());
                    }
                }
            }
        }
    }
}
